package com.tianhui.consignortwofei.mvp.presenter;

import android.content.Context;

import com.fgs.common.CommonResponse;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.google.gson.reflect.TypeToken;
import com.tianhui.consignortwofei.mvp.contract.LoginContract;
import com.tianhui.consignortwofei.mvp.model.AccountModel;
import com.tianhui.consignortwofei.mvp.model.AuthCodeModel;
import com.tianhui.consignortwofei.mvp.model.enty.CaptchaInfo;
import com.tianhui.consignortwofei.mvp.model.enty.LoginInfo;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.Map;

/**
 * @author fgs
 * @date 2020/3/27
 **/
public class LoginPresenter extends LoginContract.Presenter {

    private AccountModel mLoginModel;
    private AuthCodeModel mAuthCodeModel;
    private Context mContext;

    public LoginPresenter(Context context) {
        this.mLoginModel = new AccountModel();
        mAuthCodeModel = new AuthCodeModel();
        this.mContext = context;
    }

    @Override
    public void login(Map<String, String> map, boolean isDialog) {
        mLoginModel.loginModel(mContext, map, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                //这一步是必须的，判断view是否已经被销毁
                if (getView() != null) {

                    if (commonResponse.getCode() == 20000) {

                        LoginInfo info = JsonUtil.fromJson(commonResponse.getData().toString(), new TypeToken<LoginInfo>() {
                        }.getType());
                        getView().result(info);
                    } else {
                        getView().setMsg(commonResponse.getMsg());
                    }

                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (getView() != null) {
//                    getView().setMsg(ExceptionHandle.handleException(e).message);
                    getView().setMsg(e.message);
                }
            }
        });
    }

    @Override
    public void getAuthCode(CaptchaInfo info, boolean isDialog) {
        mAuthCodeModel.authCodeModel(mContext, info, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (null != getView()) {
                    if (commonResponse.getCode() == 20000) {
                        getView().authCodeResult();
                    } else {
                        getView().setMsg(commonResponse.getMsg());
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (null != getView()) {
                    getView().setMsg(e.message);
                }
            }
        });
    }

}
